/**
 * 用户的接口封装
 */
import request from '@/utils/request'

export const getUserList = ({ pagenum = 1, pagesize = 5,query = '' }) => {
  // const { pagenum = 1, pagesize = 5} = 你传递的那个对象
  return request({
    method: 'get',
    url: '/users',
    params: { // 传递 get 参数
      pagenum,
      pagesize,
      query // query 字段用于模糊查询用户名
    }
  }).then(res => res.data)
}

/**
 * 添加用户
 */
export const addUser = ({ username, password, email, mobile }) => request({
  method: 'post',
  url: '/users',
  data: {
    username,
    password,
    email,
    mobile
  }
}).then(res => res.data)

/**
 * 修改用户状态
 */
export const changeState = (id, state) => request({
  method: 'put',
  url: `users/${id}/state/${state}`
}).then(res => res.data)

/**
 * 根据 id 删除用户
 */
export const deleteById = id => request({
  method: 'delete',
  url: `/users/${id}`
}).then(res => res.data)

/**
 * 根据 id 查询用户信息
 */
export const getById = id => request({
  method: 'get',
  url: `/users/${id}`
}).then(res => res.data)

/**
 * 根据 id 修改用户基本信息
 */
export const updateById = (id, data) => request({
  method: 'put', // put 请求体和 post 一样
  url: `/users/${id}`,
  data: {
    email: data.email,
    mobile: data.mobile
  }
}).then(res => res.data)

/**
 * 根据用户 id 修改用户角色
 */
export const updateUserRoleByUserId = (userId, roleId) => request({
  method: 'put',
  url: `/users/${userId}I/role`,
  data: {
    rid: roleId
  }
}).then(res => res.data)
